package com.gitee.wsl.common.ui.input

import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.DecayAnimationSpec
import androidx.compose.foundation.gestures.AnchoredDraggableDefaults
import androidx.compose.foundation.gestures.AnchoredDraggableState
import androidx.compose.foundation.gestures.DraggableAnchors
import androidx.compose.foundation.gestures.DraggableAnchorsConfig
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.draganddrop.DragAndDropEvent
import androidx.compose.ui.draganddrop.DragAndDropTarget
import androidx.compose.ui.draganddrop.DragAndDropTransferAction
import androidx.compose.ui.draganddrop.DragAndDropTransferData
import androidx.compose.ui.draganddrop.DragAndDropTransferable
import androidx.compose.ui.geometry.Offset

object Drag

val Drag.AnchoredDefaults get() = AnchoredDraggableDefaults

@Composable
fun<T> Drag.anchoredState(initialValue: T)
         = remember { AnchoredDraggableState(initialValue = initialValue) }

@Composable
fun<T> Drag.anchoredState(initialValue: T, anchors: DraggableAnchors<T>)
        = remember { AnchoredDraggableState(initialValue = initialValue, anchors = anchors) }

fun<T:Any> Drag.anchors( builder: DraggableAnchorsConfig<T>.() -> Unit ) = DraggableAnchors(builder)

@OptIn(ExperimentalComposeUiApi::class)
fun Drag.transferData(transferable: DragAndDropTransferable,
                      supportedActions: Iterable<DragAndDropTransferAction>,
                      dragDecorationOffset: Offset = Offset.Zero,
                      onTransferCompleted: ((userAction: DragAndDropTransferAction?) -> Unit)? = null,
) = DragAndDropTransferData(
    transferable = transferable,
    supportedActions = supportedActions,
    dragDecorationOffset = dragDecorationOffset,
    onTransferCompleted = onTransferCompleted
    )

@Composable
fun Drag.createTarget(block:  (DragAndDropEvent) -> Boolean)
 = remember {  object : DragAndDropTarget {
                 override fun onDrop(event: DragAndDropEvent): Boolean =block(event)
           }
}